package com.gmrz.uaf.servlet.v1.filter;

import com.gmrz.uaf.server.ServerContext;
import com.gmrz.uaf.servlet.v1.common.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RequestFilter implements Filter {

    private static final Logger LOG = LogManager.getLogger(RequestFilter.class);
    private static final String CONTENT_TYPE_JSON = "application/json";
    private static final String CONTENT_TYPE_UAF = "application/fido+uaf";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        ServerContext.init((HttpServletRequest) servletRequest);
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String url = request.getRequestURL().toString();
        //验证content type
        if(url.endsWith(Constants.REQ_RECEIVE) ||
                url.endsWith(Constants.REQ_SEND) ||
                url.endsWith(Constants.AUTH_RECEIVE) ||
                url.endsWith(Constants.AUTH_SEND) ||
                url.endsWith(Constants.REQ_DELETEALL) ||
                url.endsWith(Constants.REQ_DELETE) ||
                url.endsWith(Constants.DEVICE_SUPPORT) ||
                url.endsWith(Constants.DEVICE_LIST) ||
                url.endsWith(Constants.EXTEND) ||
                url.endsWith(Constants.REQ_STATUS) ||
                url.endsWith(Constants.IDAUTH_RECEIVE) ||
                url.endsWith(Constants.IDAUTH_SEND) ||
                url.endsWith(Constants.ADD_UVI) ||
                url.endsWith(Constants.OOBAUTH_RECEIVE) ||
                url.endsWith(Constants.OOBAUTH_SEND) ||
                url.endsWith(Constants.OOBGENERATE) ||
                url.endsWith(Constants.OOBSTATUS) ||
                url.endsWith(Constants.GETUSERINFO)||
                url.endsWith(Constants.CERTGETINFO)||
                url.endsWith(Constants.CERTUPDATESTATUS)||
                url.endsWith(Constants.CERTUPDATE) ||
                url.endsWith(Constants.CERTACTIVATE) ||
                url.endsWith(Constants.GETUSERCLEARINFO) ||
                url.endsWith(Constants.DEVICE_INFO) ||
                url.endsWith(Constants.WEBAUTHN_REQ_LIST)){
            String contentType = servletRequest.getContentType();
            if ((null != contentType && contentType.contains(CONTENT_TYPE_JSON)) || (null != contentType && contentType.contains(CONTENT_TYPE_UAF))) {
                try {
                    filterChain.doFilter(servletRequest, servletResponse);
                }catch (Exception e){
                    LOG.error("uaf error",e);
                }
            } else {
                HttpServletResponse response = (HttpServletResponse) servletResponse;
                response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
            }
        }else{
            try {
                filterChain.doFilter(servletRequest, servletResponse);
            }catch (Exception e){
                LOG.error("uaf error",e);
            }
        }

    }

    @Override
    public void destroy() {

    }

}
